using System;
using System.Collections.Generic;
using System.Web.UI;

namespace MvpFramework
{
    /// <summary>
    /// A base class for Pages that implement <see cref="IMvpView"/>
    /// </summary>
    public class MvpPage : Page, IMvpView
    {
        private ValidationManager validationManager;
        private object presenter;

        
        /// <summary>
        /// All the Validation messages on the page
        /// </summary>
        public IList<IValidationMessage> ValidationErrorLabels
        {
            get { return validationManager.GetAllValidationLabels(); }
        }

        #region IMvpView Members

        /// <summary>
        /// Is this the first time the control has loaded?
        /// </summary>
        public virtual bool IsInitialLoad
        {
            get { return !IsPostBack; }
        }

        #endregion

        /// <summary>
        /// Attaches a Presenter
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            presenter = this.GetPresenter();
            EnsureChildControls();
            validationManager = new PageValidationManager(this);
            
        }

        /// <summary>
        /// Returns the Validation Label for a Specification
        /// </summary>
        /// <param name="spec"></param>
        /// <returns></returns>
        public IValidationMessage GetValidationLabelFor(object spec)
        {
            return validationManager.GetValidationLabelFor(spec);
        }
        /// <summary>
        /// Navigates to the Specified Location
        /// </summary>
        /// <param name="location"></param>
        public void NavigateTo(string location)
        {
            Response.Redirect(location);
        }
        #region Convienience Methods
        /// <summary>
        /// Safely gets a Bool from Request object
        /// </summary>
        /// <param name="paramName"></param>
        /// <returns></returns>
        public bool GetBool(string paramName)
        {
            var val = Request[paramName];
            var returnValue = false;
            bool.TryParse(val, out returnValue);
            return returnValue;
        }
        /// <summary>
        /// Returns an in from the Request. If the param is not specified will return a zero.
        /// Use GetNullableInt if 0 is not ok
        ///
        /// </summary>
        /// <param name="paramName"></param>
        /// <returns></returns>
        public int GetInt(string paramName)
        {
            var val = GetNullableInt(paramName);
            return val.HasValue ? val.Value : 0;
        }
        public int? GetNullableInt(string paramName)
        {
            var val = Request[paramName];
            int returnVal;
            if(int.TryParse(val, out returnVal))
            {
                return returnVal;
            }
            return null;
        }
        #endregion
    }
}